﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using System.Data;

namespace Util
{
    public class CargaGrid
    {
        DataGridView dgv = null;
        public CargaGrid(DataGridView dgv)
        {
            this.dgv = dgv;
        }
        private void addHeader(Object[] obj)
        {
            for (int ixH = 0; ixH < obj.Length; ixH++)
            {
                DataRow row = (DataRow)obj[ixH];
                addColGrdView(ixH.ToString(), row[0].ToString().Replace("#", "."), (Type)row[5]);
            }
        }
        public void cargaGrid(ArrayList al, bool tieneHeader)
        {
            dgv.Rows.Clear();
            dgv.Columns.Clear();
            int contador = 0;
            foreach (Object[] obj in al)
            {
                if (tieneHeader)
                {
                    contador++;
                }
                if (contador == 1)
                {
                    //Se crean las columnas
                    addHeader(obj);
                }
                else
                {
                    int ix = dgv.Rows.Add();
                    for (int ixy = 0; ixy < dgv.Columns.Count; ixy++)
                    {
                        dgv.Rows[ix].Cells[ixy].Value = obj[ixy];
                    }
                }
            }

        }
        private void addColGrdView(String nombre, String titulo)
        {
            addColGrdView(nombre, titulo, typeof(System.String));
        }
        private void addColGrdView(String nombre, String titulo, Type type)
        {
            addColGrdView(nombre, titulo, type, DataGridViewContentAlignment.NotSet);
        }
        private void addColGrdView(String nombre, String titulo, Type type, DataGridViewContentAlignment aling)
        {
            int ic = 0;
            ic = dgv.Columns.Add(nombre, titulo);
            dgv.Columns[ic].ValueType = type;
            TypeCode typeCode = Type.GetTypeCode(type);
            if (aling == DataGridViewContentAlignment.NotSet)
            {
                switch (typeCode)
                {
                    case TypeCode.Byte:
                    case TypeCode.Decimal:
                    case TypeCode.Double:
                    case TypeCode.Int16:
                    case TypeCode.Int32:
                    case TypeCode.Int64:
                    case TypeCode.SByte:
                    case TypeCode.Single:
                    case TypeCode.UInt16:
                    case TypeCode.UInt32:
                    case TypeCode.UInt64:
                        dgv.Columns[ic].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight;
                        break;
                }
            }
            else
                dgv.Columns[ic].DefaultCellStyle.Alignment = aling;
        }

    }
}
